package com.mr.web.dwr;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.context.WebApplicationContext;

import com.mr.web.support.RequestMapDataBinder;
public abstract class DWRRequestProcessor {
	public static final String ERROR_KEY = "errcode"; //������ϢKEY
   protected transient final Logger log = LoggerFactory.getLogger(getClass());
   protected static WebApplicationContext appContext;
   protected static WebApplicationContext getAppContext() {
      return appContext;
   }

   public static void setAppContext(WebApplicationContext appContext) {
      DWRRequestProcessor.appContext = appContext;
   }

   protected static WebContext getWebContext(){
      WebContext webContext = WebContextFactory.get();
      return webContext;
   }

   protected static HttpServletRequest getRequest(){
      return getWebContext().getHttpServletRequest();
   }

   protected static HttpServletResponse getResponse(){
      return getWebContext().getHttpServletResponse();
   }

   protected static HttpSession getSession(){
      return getWebContext().getSession();
   }
   protected static void setSessionAttribute(String attributeName, Object attributeValue){
      getSession().setAttribute(attributeName, attributeValue);
   }
   protected static ServletContext getApplicationContext(){
      return getSession().getServletContext();
   }
   protected static Object getSessionAttribute(String attributeName){
      return getSession().getAttribute(attributeName);
   }
   protected static BindException bindData(Object command, Map requestParam){
      RequestMapDataBinder binder = new RequestMapDataBinder(command);
      binder.bind(requestParam);
      BindException errors = new BindException(binder.getBindingResult());
      return errors;
   }
   public Session getCurrentSession(){
      return ((SessionFactory)appContext.getBean("sessionFactory")).getCurrentSession();
   }
   public Session getBlogCurrentSession(){
   	return ((SessionFactory)appContext.getBean("blogSessionFactory")).getCurrentSession();
   }
   public String getContextPath(){
      return getRealPath("/");
   }
   public String getRealPath(String filename){
      return appContext.getServletContext().getRealPath(filename);
   }
}
